home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / CharConvSrc.lha / CharConv / CharConv16.c next >
Encoding:
C/C++ Source or Header  |  1996-09-11  |  6.3 KB  |  267 lines

  1. #include <stdarg.h>
  2. #include <varargs.h>
  3.  
  4. #include <exec/types.h>
  5. #include <exec/execbase.h>
  6. #include <dos/dos.h>
  7. #include <dos/dostags.h>
  8. #include <dos/rdargs.h>
  9.  
  10. #include <CharTabs.h>
  11.  
  12. #define VERSION "1.6"
  13.  
  14. UBYTE *ver="\0$VER: CharConv "VERSION" ("__COMMODORE_DATE__")";
  15.  
  16. #define BUFSIZE 4096
  17.  
  18. UBYTE ibuf[BUFSIZE];
  19. UBYTE obuf[2*BUFSIZE];
  20.  
  21. UBYTE readfrom[256];
  22. UBYTE readto[256];
  23.  
  24. #define EOL_CR   1
  25. #define EOL_LF   2
  26. #define EOL_CRLF 3
  27.  
  28. extern struct ExecBase *SysBase;
  29.  
  30. #define FROM            0
  31. #define TO              1
  32. #define FROMCHRS        2
  33. #define TOCHRS          3
  34. #define LF              4
  35. #define CR              5
  36. #define CRLF            6
  37. #define NOANSI          7
  38.  
  39. UBYTE *argstr="FROM/A,TO/A,FROMCHRS/A,TOCHRS/A,LF/S,CR/S,CRLF/S,NOANSI/S";
  40. ULONG argarray[8];
  41.  
  42. UBYTE *helptxt = "\n\x1b[4m\x1b[1mCharConv "VERSION"\x1b[24m\x1b[22m © 1994 Johan Billing - \x1b[3mFREEWARE!\x1b[23m\n\n"
  43.                  "Usage: CharConv <from> <to> <fromchrs> <tochrs> [LF/CR/CRLF/NOANSI]\n\n"
  44.                  " Built-in charsets: IBM=PC, AMIGA=ISO, SIS=SF7, MAC\n"
  45.                  " (Use '#' in front of the charset to force loading of *.crossdos file)\n\n";
  46.  
  47. myputs(UBYTE *str)
  48. {
  49.    Write(Output(),str,strlen(str));
  50. }
  51.  
  52. void smallsprintf(UBYTE *buffer,UBYTE *ctl, ...)
  53. {
  54.    va_list args;
  55.  
  56.    va_start(args, ctl);
  57.    RawDoFmt(ctl, args, (void (*))"\x16\xc0\x4e\x75", buffer);
  58.    va_end(args);
  59. }
  60.  
  61. void _main(void)
  62. {
  63.    BPTR ifp,ofp,rfp;
  64.    UBYTE buffer[100],buf2[100];
  65.  
  66.    UBYTE *fchrs=NULL;
  67.    UBYTE *tchrs=NULL;
  68.  
  69.    BOOL breakflag=FALSE;
  70.  
  71.    UWORD ipos,opos,ilen,c=0,lastc=0,eol=0;
  72.    BOOL noansi=FALSE;
  73.    BOOL ansinow=FALSE,onemore=FALSE;
  74.  
  75.    struct RDArgs *rdargs;
  76.    struct RDArgs *myrdargs;
  77.  
  78.    if(SysBase->LibNode.lib_Version<37)
  79.    {
  80.       myputs("Sorry! This program requires Kickstart 2.04 or higher\n");
  81.       _exit(0);
  82.    }
  83.  
  84.    myrdargs = (struct RDArgs *)AllocDosObjectTags(DOS_RDARGS, TAG_DONE);
  85.  
  86.    if(!myrdargs)
  87.    {
  88.       PrintFault(IoErr(),NULL);
  89.       _exit(10);
  90.    }
  91.  
  92.    myrdargs->RDA_ExtHelp = helptxt;
  93.  
  94.    if(!(rdargs = (struct RDArgs *)ReadArgs(argstr,argarray,myrdargs)))
  95.    {
  96.       PrintFault(IoErr(),NULL);
  97.       FreeDosObject(DOS_RDARGS,myrdargs);
  98.       _exit(10);
  99.    }
  100.  
  101.    if(stricmp(argarray[FROMCHRS],"IBM")==0 || stricmp(argarray[FROMCHRS],"PC")==0)
  102.       fchrs=IbmToAmiga;
  103.  
  104.    else if(stricmp(argarray[FROMCHRS],"SF7")==0 || stricmp(argarray[FROMCHRS],"SIS")==0)
  105.       fchrs=SF7ToAmiga;
  106.  
  107.    else if(stricmp(argarray[FROMCHRS],"MAC")==0)
  108.       fchrs=MacToAmiga;
  109.  
  110.    else if(!(stricmp(argarray[FROMCHRS],"ISO")==0 || stricmp(argarray[FROMCHRS],"AMIGA")==0))
  111.    {
  112.       if(((UBYTE *)argarray[FROMCHRS])[0]=='#') strcpy(buf2,&((UBYTE *)argarray[FROMCHRS])[1]);
  113.       else                                      strcpy(buf2,argarray[FROMCHRS]);
  114.  
  115.       smallsprintf(buffer,"L:FileSystem_Trans/%.50s.crossdos",buf2);
  116.  
  117.       if(!(rfp=Open(buffer,MODE_OLDFILE)))
  118.       {
  119.          Printf("Unknown charset \"%s\"\n",buf2);
  120.          FreeArgs(rdargs);
  121.          FreeDosObject(DOS_RDARGS,myrdargs);
  122.          _exit(0);
  123.       }
  124.       Seek(rfp,256,OFFSET_BEGINNING);
  125.       Read(rfp,readfrom,256);
  126.       Close(rfp);
  127.  
  128.       fchrs=readfrom;
  129.    }
  130.  
  131.    if(stricmp(argarray[TOCHRS],"IBM")==0 || stricmp(argarray[TOCHRS],"PC")==0)
  132.       tchrs=AmigaToIbm;
  133.  
  134.    else if(stricmp(argarray[TOCHRS],"SF7")==0 || stricmp(argarray[TOCHRS],"SIS")==0)
  135.       tchrs=AmigaToSF7;
  136.  
  137.    else if(stricmp(argarray[TOCHRS],"MAC")==0)
  138.       tchrs=AmigaToMac;
  139.  
  140.    else if(!(stricmp(argarray[TOCHRS],"ISO")==0 || stricmp(argarray[TOCHRS],"AMIGA")==0))
  141.    {
  142.       if(((UBYTE *)argarray[TOCHRS])[0]=='#') strcpy(buf2,&((UBYTE *)argarray[TOCHRS])[1]);
  143.       else                                    strcpy(buf2,argarray[TOCHRS]);
  144.  
  145.       smallsprintf(buffer,"L:FileSystem_Trans/%.50s.crossdos",buf2);
  146.  
  147.       if(!(rfp=Open(buffer,MODE_OLDFILE)))
  148.       {
  149.          Printf("Unknown charset \"%s\"\n",buf2);
  150.          FreeArgs(rdargs);
  151.          FreeDosObject(DOS_RDARGS,myrdargs);
  152.          _exit(0);
  153.       }
  154.       Read(rfp,readto,256);
  155.       Close(rfp);
  156.  
  157.       tchrs=readto;
  158.    }
  159.  
  160.    if(argarray[LF])
  161.       eol=EOL_LF;
  162.  
  163.    if(argarray[CR])
  164.       eol=EOL_CR;
  165.  
  166.    if(argarray[CRLF])
  167.       eol=EOL_CRLF;
  168.  
  169.    if(argarray[NOANSI])
  170.       noansi=TRUE;
  171.  
  172.    if(!(ifp=(BPTR)Open(argarray[FROM],MODE_OLDFILE)))
  173.    {
  174.       Printf("Error opening \"%s\" for reading!\n",argarray[FROM]);
  175.       FreeArgs(rdargs);
  176.       FreeDosObject(DOS_RDARGS,myrdargs);
  177.       _exit(0);
  178.    }
  179.  
  180.    if(!(ofp=(BPTR)Open(argarray[TO],MODE_NEWFILE)))
  181.    {
  182.       Printf("Error opening \"%s\" for writing!\n",argarray[TO]);
  183.       Close(ifp);
  184.       FreeArgs(rdargs);
  185.       FreeDosObject(DOS_RDARGS,myrdargs);
  186.       _exit(0);
  187.    }
  188.  
  189.    c=0;
  190.  
  191.    while((ilen=Read(ifp,ibuf,BUFSIZE)) && !breakflag)
  192.    {
  193.       opos=0;
  194.  
  195.       for(ipos=0;ipos<ilen;ipos++)
  196.       {
  197.          lastc=c;
  198.          c=ibuf[ipos];
  199.  
  200.          if(c==10 || c==13)
  201.          {
  202.             if(!eol) obuf[opos++]=c;
  203.             else
  204.             {
  205.                if((c==10 && lastc!=13) || c==13)
  206.                {
  207.                   switch(eol)
  208.                   {
  209.                      case EOL_CR:   obuf[opos++]=13;
  210.                                     break;
  211.                      case EOL_LF:   obuf[opos++]=10;
  212.                                     break;
  213.                      case EOL_CRLF: obuf[opos++]=13;
  214.                                     obuf[opos++]=10;
  215.                                     break;
  216.                   }
  217.                }
  218.             }
  219.          }
  220.          else if((c==0x1b || c==0x9b) && noansi==TRUE || ansinow)
  221.          {
  222.             if(onemore)
  223.             {
  224.                ansinow=FALSE;
  225.                onemore=FALSE;
  226.             }
  227.             else if(c=='#')
  228.             {
  229.                onemore=TRUE;
  230.             }
  231.             else if((c>='@' && c<='Z') || (c>='a' && c<='z'))
  232.             {
  233.                if(!(c=='P' && (lastc=='['||lastc==';')))
  234.                   ansinow=FALSE;
  235.             }
  236.             else if(c=='6' && lastc=='(')
  237.             {
  238.                ansinow=FALSE;
  239.             }
  240.             else
  241.             {
  242.                ansinow=TRUE;
  243.             }
  244.          }
  245.          else
  246.          {
  247.             if(fchrs) c=fchrs[c];
  248.             if(tchrs) c=tchrs[c];
  249.             if(c) obuf[opos++]=c;
  250.          }
  251.        }
  252.        if(opos) Write(ofp,obuf,opos);
  253.  
  254.        if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) breakflag=TRUE;
  255.    }
  256.  
  257.    Close(ifp);
  258.    Close(ofp);
  259.    FreeArgs(rdargs);
  260.    FreeDosObject(DOS_RDARGS,myrdargs);
  261.  
  262.    if(breakflag) Printf("*** Break\n");
  263.  
  264.    _exit(0);
  265. }
  266.  
  267.